/*  
 * @(#) DateHelper.java Create on 2011-12-14 下午3:38:44   
 *   
 * Copyright 2011 by xl.   
 */

package com.doeis.tools;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 
 * @author wxy
 *
 */
public class DateHelper {

	private String minDate;
	private String maxDate;
	private static SimpleDateFormat secFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static SimpleDateFormat dayFormat = new SimpleDateFormat("yyyy-MM-dd");
	private static SimpleDateFormat mdFormat = new SimpleDateFormat("MM-dd");
	private static SimpleDateFormat hmFormat = new SimpleDateFormat("HH:mm");
	private final static long dayTime = 1000 * 60 * 60 * 24;

	public String getMaxDate() {
		return maxDate;
	}

	public String getMinDate() {
		return minDate;
	}

	public static String formatDateToStr(int date) {
		SimpleDateFormat simpleDateFormat4Time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return simpleDateFormat4Time.format(date * 1000);
	}

	public void setDateType(int type) {
		Date date = new Date();
		Date today = new Date(System.currentTimeMillis());
		String mixDate = "";
		String maxDate = "";
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
		maxDate = simpleDateFormat.format(today).toString();
		if (type == 1) {
			// 3天内
			date = new Date(System.currentTimeMillis() - (0x5265c00L * (3 - 1)));
			mixDate = simpleDateFormat.format(date).toString();
		} else if (type == 2) {
			// 一周内
			date = new Date(System.currentTimeMillis() - (0x5265c00L * (7 - 1)));
			mixDate = simpleDateFormat.format(date).toString();
		} else if (type == 3) {
			// 一月内
			date = new Date(System.currentTimeMillis() - (0x5265c00L * (30 - 1)));
			mixDate = simpleDateFormat.format(date).toString();
		} else if (type == 4) {
			// 三月内
			date = new Date(System.currentTimeMillis() - (0x5265c00L * (90 - 1)));
			mixDate = simpleDateFormat.format(date).toString();
		}
		this.maxDate = maxDate;
		this.minDate = mixDate;
		// return simpleDateFormat.format(date);
	}

	public void setDateType(int type, String datetype) {
		Date date = new Date();
		Date today = new Date(System.currentTimeMillis());
		String mixDate = "";
		String maxDate = "";
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(datetype);
		maxDate = simpleDateFormat.format(today).toString();
		if (type == 1) {
			// 3天内
			date = new Date(System.currentTimeMillis() - (0x5265c00L * (3 - 1)));
			mixDate = simpleDateFormat.format(date).toString();
		} else if (type == 2) {
			// 一周内
			date = new Date(System.currentTimeMillis() - (0x5265c00L * (7 - 1)));
			mixDate = simpleDateFormat.format(date).toString();
		} else if (type == 3) {
			// 一月内
			date = new Date(System.currentTimeMillis() - (0x5265c00L * (30 - 1)));
			mixDate = simpleDateFormat.format(date).toString();
		} else if (type == 4) {
			// 三月内
			date = new Date(System.currentTimeMillis() - (0x5265c00L * (90 - 1)));
			mixDate = simpleDateFormat.format(date).toString();
		} else if (type == 0) {
			date = new Date(System.currentTimeMillis() - (0x5265c00L * (0 - 1)));
			maxDate = simpleDateFormat.format(date).toString();
			mixDate = simpleDateFormat.format(today).toString();
		}
		this.maxDate = maxDate;
		this.minDate = mixDate;
	}

	/**
	 * 改变日志格式
	 * 
	 * @param datetime
	 * @param oldFormat
	 * @param newFormat
	 * @return
	 */
	public static String formatDateString(String datetime, String oldFormat, String newFormat) {
		Date date = null;
		try {
			if (datetime == null || datetime.equals("")) {
				return "";
			}
			SimpleDateFormat formatter = new SimpleDateFormat(oldFormat);
			date = formatter.parse(datetime);

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return getSpecifiedDate(date.getTime(), newFormat);
	}

	/**
	 * 根据指定的格式显示时间
	 * 
	 * @param time
	 *            long
	 * @param format
	 *            String author xieyan
	 * @return String
	 */
	public static String getSpecifiedDate(long time, String format) {
		if (time == 0) {
			return "-";
		}

		Date timeVal = new Date(time);

		String strTime = null;
		try {
			SimpleDateFormat simpledateformat = new SimpleDateFormat(format);
			strTime = simpledateformat.format(timeVal);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return strTime;
	}

	/**
	 * 格式 ： 输出指定格式类型的时间
	 * 
	 * @param formater
	 *            String author xieyan
	 * @return String
	 */
	public static String getNowDate(String formater) {
		String strTime = null;
		try {
			SimpleDateFormat simpledateformat = new SimpleDateFormat(formater);
			strTime = simpledateformat.format(new Date());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return strTime;
	}

	public static void main(String args[]) {
		/*
		 * DateHelper dh = new DateHelper(); dh.setDateType(0,"yyyy-MM-dd");
		 * System.out.println(dh.maxDate); System.out.println(dh.minDate);
		 */
		DateHelper.formatStringToLong("2010-10-07 12:31:00", DateHelper.FMT_DATE_DATETIME3);
		System.out.println(DateHelper.getDayEndSecond("2010-10-07"));
		System.out.println("------" + ("  aa  ".trim()) + "------");
	}

	/**
	 * string转换成long时间
	 * 
	 * @param dateString
	 * @param format
	 * @return
	 */
	public static long formatStringToLong(String dateString, String format) {
		long result = 0l;
		SimpleDateFormat simpledateformat = new SimpleDateFormat(format);
		try {
			result = simpledateformat.parse(dateString).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return result;
	}

	public static String addDate(String strDate, int days) throws ParseException {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date cDate = df.parse(strDate);
		GregorianCalendar gcalendar = new GregorianCalendar();
		gcalendar.setTime(cDate);
		gcalendar.add(Calendar.DATE, days);
		return df.format(gcalendar.getTime()).toString();
	}

	public static String getDayEndSecond(String dateTime) {
		// String result = "";
		if (!dateTime.equals("")) {
			try {
				dateTime = DateHelper.formatDateString(dateTime + " 23:59:59", DateHelper.FMT_DATE_DATETIME, FMT_DATE_DATETIME);
			} catch (Exception e) {
			}
		}

		return dateTime;
	}

	public static Timestamp getTimestampNowDate() {
		Timestamp timestamp = null;
		try {
			timestamp = new Timestamp(new Date().getTime());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return timestamp;
	}

	private static SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy-MM");

	/**
	 * 获取某个日期的前一月份(包含天)
	 * 
	 * @param datestr
	 * @return
	 */
	public static String getPreMonthDate(String datestr, String sdf) {
		try {
			if (StringHelper.isNull(sdf)) {
				return null;
			}
			SimpleDateFormat ym = new SimpleDateFormat(sdf);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(ym.parse(datestr));
			calendar.add(Calendar.MONTH, -1);

			return ym.format(calendar.getTime());

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取某个月份的第一天
	 * 
	 * @param date
	 *            输入的日期
	 * @param num
	 *            如为-1，则表示为上个月； 为0，则是本月
	 */
	public static Date firstDayOfMonth(Date date, Integer num) {
		Date firstDay = null;
		try {
			Calendar ca = Calendar.getInstance();
			ca.setTime(date);
			ca.set(Calendar.DATE, 1);
			ca.add(Calendar.MONTH, num);
			firstDay = new Date(ca.getTimeInMillis());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return firstDay;
	}

	// 获取一个月的最后一天
	public static Date lastDayOfMonth(Date date) {
		Date endDate = null;
		try {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			int endday = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
			calendar.set(Calendar.DATE, endday);
			endDate = new Date(calendar.getTimeInMillis());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return endDate;
	}

	// 获取日期的月份值
	public static Integer getMonthOfDate(Date date) {
		try {
			Calendar ca = Calendar.getInstance();
			ca.setTime(date);
			return ca.get(Calendar.MONTH) + 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取某个日期的前一月份
	 * 
	 * @param datestr
	 * @return
	 */
	public static String getPreMonth(String datestr) {
		try {
			SimpleDateFormat ym = new SimpleDateFormat("yyyy-MM");
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(ym.parse(datestr));
			calendar.add(Calendar.MONTH, -1);

			return ym.format(calendar.getTime());

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取某个日期的前一月份
	 * 
	 * @param datestr
	 * @return
	 */
	public static Date getPreMonth(Date datestr) {
		try {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(datestr);
			calendar.add(Calendar.MONTH, -1);

			return new Date(calendar.getTime().getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取某个日期的下一月份
	 * 
	 * @param datestr
	 * @return
	 */
	public static String getNextMonth(String datestr) {
		try {
			SimpleDateFormat ym = new SimpleDateFormat("yyyy-MM");
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(ym.parse(datestr));
			calendar.add(Calendar.MONTH, 1);

			return ym.format(calendar.getTime());

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 
	 * @author wxy
	 * @param times
	 * @param day
	 * @return
	 * @date 2014年7月9日 上午9:54:20
	 */
	public static String getDateByDay(long times, int day) {
		String temp = day + "";
		if (day < 10) {
			temp = "0" + day;
		}
		return monthFormat.format(times) + "-" + temp;
	}

	public static String format(Timestamp time) {
		return format(time.getTime());
	}

	public static String format(long time) {
		// 今天的日期
		String date = dayFormat.format(System.currentTimeMillis());
		date += " 00:00:00";

		long firsts = 0; // 今天的第一秒
		try {
			firsts = secFormat.parse(date).getTime();
			Calendar c = Calendar.getInstance();
			c.setTimeInMillis(time);
			if (time >= firsts) {// 今天
				return hmFormat.format(c.getTime()); // HH:mm

			} else if (time >= (firsts - dayTime)) {// 昨天
				return "昨天";

			} else if (time >= (firsts - dayTime * 2)) {// 前天
				return "前天";

			} else {// 以前的
				return mdFormat.format(c.getTime());// MM-dd
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return "--";
	}

	public static Timestamp currentDateTime() {
		return new Timestamp(System.currentTimeMillis());
	}
	/**
	 * 增加几天
	 * 
	 * @param day
	 * @return
	 * @author wxy 2016年3月22日星期二中国标准时间上午11:56:10
	 */
	public static Timestamp addTime(int day) {
		return new Timestamp(System.currentTimeMillis()+day*24*60*60*1000);
	}

	public static final String FMT_DATE_YYYY = "yyyy";
	public static final String FMT_DATE_YYMMDD = "yyMMdd";
	public static final String FMT_DATE_YYYYMMDD = "yyyyMMdd";
	public static final String FMT_DATE_YYYY_MM_DD = "yyyy-MM-dd";
	public static final String FMT_DATE_YYYY_MM_DD2 = "yyyy/MM/dd";
	public static final String FMT_DATE_DATETIME = "yyyy-MM-dd HH:mm:ss";
	public static final String FMT_DATE_DATETIME3 = "yyyy-MM-dd HH:mm:sss";
	public static final String FMT_DATE_DATETIME2 = "yyyy-MM-dd HH:mm";
	public static final String FMT_DATE_DATETIME_TIGHT = "yyyyMMddHHmmss";
	public static final String FMT_DATE_YY_MM = "yy-MM";
	public static final String FMT_DATE_MM = "MM";
	public static final String FMT_DATE_DD = "dd";
	public static final String FMT_DATE_TIME = "HH:mm:ss";

}
